﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using SageKing.Database.EF.Entity.Contracts;

namespace SageKing.Database.EF.Entity;

public class TemplateStoreOptions
{
    public Action<DbContextOptionsBuilder> TemplateContext { get; set; }
    public Action<IServiceProvider, DbContextOptionsBuilder> ResolveDbContextOptions { get; set; }
}

public interface ITemplateContext
{
    DbSet<TemplateBase> Templates { get; set; }
    DbSet<TenantTemplateDataBase> TenantTemplateDatas { get; set; }
    DbSet<TemplateConfigBase> TemplateConfigs { get; set; }
}

public class TemplateContext : TemplateContext<TemplateContext>
{
    public TemplateContext(DbContextOptions<TemplateContext> options)
      : base(options)
    { }
}

public class TemplateContext<TContext> : DbContext, ITemplateContext
  where TContext : DbContext, ITemplateContext
{
    public TemplateContext(DbContextOptions<TContext> options)
      : base(options)
    { }

    public DbSet<TemplateBase> Templates { get; set; }
    public DbSet<TenantTemplateDataBase> TenantTemplateDatas { get; set; }
    public DbSet<TemplateConfigBase> TemplateConfigs { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.ApplyConfiguration(new TemplateConfiguration());
        modelBuilder.ApplyConfiguration(new TemplateConfigConfiguration());
        modelBuilder.ApplyConfiguration(new TenantTemplateDataConfiguration());
    }
}
